package com.zhipu.balance.ui.vm.home.calculate.dami

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.SPUtils
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordDaMi
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 大米不完善粒
 */
class DaMiFaultinessViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 大米实体
     */
    var mRecordDaMi : RecordDaMi? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 不完善粒-试样质量
     */
    var qualityOfImperfectGrainSample = ObservableField("")


    /**
     * 不完善粒质量
     */
    var imperfectGrainQuality =  ObservableField("")





    /**
     * 虫蚀粒质量
     */
    val insectBoredQuality = ObservableField("")

    /**
     * 病斑粒质量
     */
    val mottledQuality = ObservableField("")
    /**
     * 生霉粒重量
     */
    var moldWeight = ObservableField("")
    /**
     * 未熟粒重量
     */
    var immatureGrain = ObservableField("")
    /**
     * 糙米粒重量
     */
    var huskedRice = ObservableField("")





    /**
     * 未熟粒率-结果
     */
    var immatureGrainResult = ObservableField("")
    /**
     * 虫蚀率-结果
     */
    var insectBoredResult = ObservableField("")
    /**
     * 病斑率-结果
     */
    var mottledResult = ObservableField("")
    /**
     * 生霉粒率-结果
     */
    var moldResult = ObservableField("")

    /**
     * 糙米粒率-结果+
     */
    var huskedRiceResult = ObservableField("")








    /**
     * 不完善粒-结果
     */
    var resultOfImperfectGrain =  ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordDaMi?.imperfectGrainRecorder)) {
            //不完善粒-试样质量
            qualityOfImperfectGrainSample.set(mRecordDaMi?.qualityOfImperfectGrainSample.toString())
            //不完善粒质量
            imperfectGrainQuality.set(mRecordDaMi?.imperfectGrainQuality.toString())


            //虫蚀粒质量
            insectBoredQuality.set(mRecordDaMi?.insectBoredQuality.toString())
            //病斑粒质量
            mottledQuality.set(mRecordDaMi?.mottledQuality.toString())
            //生霉粒重量
            moldWeight.set(mRecordDaMi?.moldWeight.toString())
            //未熟粒重量
            immatureGrain.set(mRecordDaMi?.immatureBrokenGrainQuality.toString())
            //糙米粒重量
            huskedRice.set(mRecordDaMi?.incompleteBrownRiceQuality.toString())



            //虫蚀率-结果
            insectBoredResult.set(mRecordDaMi?.insectBoredResult.toString())
            //病斑率-结果
            mottledResult.set(mRecordDaMi?.mottledResult.toString())
            //生霉粒率-结果
            moldResult.set(mRecordDaMi?.moldResult.toString())
            //未熟粒率-结果
            immatureGrainResult.set(mRecordDaMi?.immatureGrainResult.toString())
            //糙米粒率-结果
            huskedRiceResult.set(mRecordDaMi?.rougheningResults.toString())




            //不完善粒-结果
            resultOfImperfectGrain.set(mRecordDaMi?.resultOfImperfectGrain.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //不完善粒-试样质量
        qualityOfImperfectGrainSample.set("")
        //不完善粒质量
        imperfectGrainQuality.set("")


        //虫蚀粒质量
        insectBoredQuality.set("")
        //病斑粒质量
        mottledQuality.set("")
        //生霉粒重量
        moldWeight.set("")
        //未熟粒重量
        immatureGrain.set("")
        //糙米粒重量
        huskedRice.set("")


        //虫蚀率-结果
        insectBoredResult.set("")
        //病斑率-结果
        mottledResult.set("")
        //生霉粒率-结果
        moldResult.set("")
        //未熟粒率-结果
        immatureGrainResult.set("")
        //糙米粒率-结果
        huskedRiceResult.set("")


        //不完善粒-结果
        resultOfImperfectGrain.set("")
    }




    /**
     * 试样质量
     */
    fun BulkQuality(){
        qualityOfImperfectGrainSample.set(weighing)
        mRecordDaMi?.qualityOfImperfectGrainSample = qualityOfImperfectGrainSample.get()?.toDouble()
    }

    /**
     * 不完善粒质量
     */
    fun ImperfectGrainQuality(){
        imperfectGrainQuality.set(weighing)
        mRecordDaMi?.imperfectGrainQuality = imperfectGrainQuality.get()?.toDouble()
    }


    /**
     * 未熟粒重量
     */
    fun immatureKernel(){
        immatureGrain.set(weighing)
        mRecordDaMi?.immatureBrokenGrainQuality = immatureGrain.get()?.toDouble()
    }



    /**
     * 虫蚀粒质量
     */
    fun insectBoredQuality(){
        insectBoredQuality.set(weighing)
        mRecordDaMi?.insectBoredQuality = insectBoredQuality.get()?.toDouble()
    }


    /**
     * 病斑粒质量
     */
    fun mottledQuality(){
        mottledQuality.set(weighing)
        mRecordDaMi?.mottledQuality = mottledQuality.get()?.toDouble()
    }

    /**
     * 生霉粒重量
     */
    fun moldWeight(){
        moldWeight.set(weighing)
        mRecordDaMi?.moldWeight = moldWeight.get()?.toDouble()
    }


    /**
     * 糙米粒重量
     */
    fun huskedRice(){
        huskedRice.set(weighing)
        mRecordDaMi?.incompleteBrownRiceQuality = huskedRice.get()?.toDouble()
    }




    /**
     * 计算
     */
    fun calculation(){
        /**
         * 是否细分
         */
        if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){
            /**
             * 未熟粒率-结果
             */
            var ImmatureGrainResult = CommonUtils.formatDouble(mRecordDaMi?.immatureBrokenGrainQuality!! / mRecordDaMi?.qualityOfImperfectGrainSample!! * 100)
            mRecordDaMi?.immatureGrainResult = ImmatureGrainResult
            immatureGrainResult.set(ImmatureGrainResult.toString())
            /**
             * 虫蚀率-结果
             */
            var InsectBoredResult = CommonUtils.formatDouble(mRecordDaMi?.insectBoredQuality!! / mRecordDaMi?.qualityOfImperfectGrainSample!! * 100)
            mRecordDaMi?.insectBoredResult = InsectBoredResult
            insectBoredResult.set(InsectBoredResult.toString())
            /**
             * 病斑率-结果
             */
            var MottledResult = CommonUtils.formatDouble(mRecordDaMi?.mottledQuality!! / mRecordDaMi?.qualityOfImperfectGrainSample!! * 100)
            mRecordDaMi?.mottledResult = MottledResult
            mottledResult.set(MottledResult.toString())
            /**
             * 赤霉病粒-结果
             */
            var MoldResult = CommonUtils.formatDouble(mRecordDaMi?.moldWeight!! / mRecordDaMi?.qualityOfImperfectGrainSample!! * 100)
            mRecordDaMi?.moldResult = MoldResult
            moldResult.set(MoldResult.toString())
            /**
             * 糙米粒率-结果
             */
            var HuskedRiceResult = CommonUtils.formatDouble(mRecordDaMi?.incompleteBrownRiceQuality!! / mRecordDaMi?.qualityOfImperfectGrainSample!! * 100)
            mRecordDaMi?.rougheningResults = HuskedRiceResult
            huskedRiceResult.set(HuskedRiceResult.toString())

            /**
             *  不完善粒-结果
             */
            var ResultOfImperfectGrain = ImmatureGrainResult + InsectBoredResult + MottledResult + MoldResult + HuskedRiceResult;
            mRecordDaMi?.resultOfImperfectGrain = ResultOfImperfectGrain
            resultOfImperfectGrain.set(ResultOfImperfectGrain.toString())
        }else{
            /**
             * 不完善粒-结果
             */
            var mResultOfImperfectGrain: Double = CommonUtils.formatDouble(mRecordDaMi!!.imperfectGrainQuality / mRecordDaMi!!.qualityOfImperfectGrainSample * 100)
            mRecordDaMi?.resultOfImperfectGrain = mResultOfImperfectGrain
            resultOfImperfectGrain.set(mResultOfImperfectGrain.toString())

        }
        /**
         * 不完善粒记录员
         */
        mRecordDaMi!!.imperfectGrainRecorder = mPersonnel!!.name


        //不完善粒-试样质量
        if(mRecordDaMi?.qualityOfImperfectGrainSample == 0.00){
            qualityOfImperfectGrainSample.set("0.00")
        }


        //未熟粒重量
        if(mRecordDaMi?.immatureBrokenGrainQuality == 0.00){
            immatureGrain.set("0.00")
        }
        //虫蚀粒质量
        if(mRecordDaMi?.insectBoredQuality == 0.00){
            insectBoredQuality.set(mRecordDaMi?.insectBoredQuality.toString())
        }
        //病斑粒质量
        if(mRecordDaMi?.mottledQuality == 0.00){
            mottledQuality.set("0.00")
        }
        //生霉粒重量
        if(mRecordDaMi?.moldWeight == 0.00){
            moldWeight.set("0.00")
        }
        //糙米粒重量
        if(mRecordDaMi?.incompleteBrownRiceQuality == 0.00){
            huskedRice.set("0.00")
        }
        //不完善粒质量
        if(mRecordDaMi?.imperfectGrainQuality == 0.00){
            imperfectGrainQuality.set("0.00")
        }
    }
}